//
//  MusicCategoryController.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/8.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "MusicCategoryController.h"
#import "CHTCollectionViewWaterfallLayout.h"
#import "FallCollectionViewCell.h"
#import "MCategoryBLL.h"
#import "CategoryGridView.h"

#define HEADER_IDENTIFIER @"WaterfallHeader"

@interface MusicCategoryController () <UICollectionViewDataSource,UICollectionViewDelegate,CHTCollectionViewDelegateWaterfallLayout> {

    CGFloat cellWidth;
}
@property (nonatomic,strong) UICollectionView *categoryCollectionView;
@property (nonatomic,strong) UIScrollView *categoryScrollView;
@property (nonatomic,strong) MCategoryBLL *categoryBLL;
@end

@implementation MusicCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分类";
    
    
    //[self.view addSubview:self.categoryCollectionView];
    [self.view addSubview:self.categoryScrollView];
    [self.categoryBLL loadCategory:^{
        
        __block CGFloat topY = 10;
        [self.categoryBLL.tagList enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL * _Nonnull stop) {
            
            CategoryGridView *gridView = [[CategoryGridView alloc] initWithFrame:CGRectMake(5, topY, self.view.width - 10, 50)];
            NSArray *source = [self.categoryBLL.categoryList objectForKey:key];
            gridView.dataKey = key;
            gridView.dataSource = source;
            [self.categoryScrollView addSubview:gridView];
            
            topY += gridView.height + 10;
            
        }];
        self.categoryScrollView.contentSize = CGSizeMake(self.view.width, topY + 10);
        
        
        //[self.categoryCollectionView reloadData];
    }];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -懒加载

- (MCategoryBLL *)categoryBLL {

    if (!_categoryBLL) {
        _categoryBLL = [[MCategoryBLL alloc] init];
    }
    return _categoryBLL;
}

- (UIScrollView *)categoryScrollView{

    if(!_categoryScrollView){
        
        _categoryScrollView=[[UIScrollView alloc] initWithFrame:self.view.bounds];
        _categoryScrollView.showsHorizontalScrollIndicator = NO;
        _categoryScrollView.showsVerticalScrollIndicator = YES;
        _categoryScrollView.pagingEnabled = YES;
    }
    return _categoryScrollView;
}

- (UICollectionView *)categoryCollectionView{
    
    if (!_categoryCollectionView) {
        
        CHTCollectionViewWaterfallLayout *flowlayOut = [[CHTCollectionViewWaterfallLayout alloc] init];
        flowlayOut.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        //flowlayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        //flowlayOut.headerHeight = 10;
        flowlayOut.columnCount = 4;
        flowlayOut.minimumColumnSpacing = 0; //行间距
        flowlayOut.minimumInteritemSpacing = 0;//列间距
        
        cellWidth= (self.view.width - flowlayOut.sectionInset.left - flowlayOut.sectionInset.right -flowlayOut.minimumInteritemSpacing * 3)/4;
        //flowlayOut.itemSize = CGSizeMake(width, 111);
        //NSLog(@"cellWidth =%f",cellWidth);
        _categoryCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowlayOut];
        _categoryCollectionView.dataSource = self;
        _categoryCollectionView.delegate = self;
        _categoryCollectionView.backgroundColor = [UIColor whiteColor];
        
        [_categoryCollectionView registerClass:[FallCollectionViewCell class] forCellWithReuseIdentifier:kMusicCategoryListCellIdentifier];
        //[_categoryCollectionView registerClass:[CHTCollectionViewWaterfallHeader class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:HEADER_IDENTIFIER];
    }
    return _categoryCollectionView;
}

#pragma mark - UICollectionViewDataSource Methods

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return [self.categoryBLL.tagList count];
    //return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    NSString *key = [self.categoryBLL.tagList objectAtIndex:section];
    return [[self.categoryBLL.categoryList objectForKey:key] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *key = [self.categoryBLL.tagList objectAtIndex:indexPath.section];
    NSArray *source = [self.categoryBLL.categoryList objectForKey:key];
    
    
    FallCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:kMusicCategoryListCellIdentifier forIndexPath:indexPath];;
    cell.titleLabel.text = source[indexPath.row];
    //cell.backgroundColor = [UIColor randomColor];
    return cell;
}

// 允许选中时，高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}
// 高亮完成后回调
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __FUNCTION__);
    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
}
// 由高亮转成非高亮完成时的回调
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __FUNCTION__);
    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
}

// 设置是否允许选中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __FUNCTION__);
    return YES;
}
// 设置是否允许取消选中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __FUNCTION__);
    return YES;
}
/**
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView = nil;
    
    if ([kind isEqualToString:CHTCollectionElementKindSectionHeader]) {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                          withReuseIdentifier:HEADER_IDENTIFIER
                                                                 forIndexPath:indexPath];
    }
    
    return reusableView;
}
 **/

#pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *key = [self.categoryBLL.tagList objectAtIndex:indexPath.section];
    NSArray *source = [self.categoryBLL.categoryList objectForKey:key];
    
    NSInteger row = ([source count] -1) % 3 == 0? ([source count] -1) / 3 : ([source count] -1) / 3 + 1;
    
    CGFloat height = indexPath.row == 0? row*50:50;
    if ([source count] <= 4) {
        height = 50;
    }
    
    return CGSizeMake(cellWidth, height);
}

@end
